package com.xmhmyh.generalframework.framework.redisQueue;

import com.xmhmyh.generalframework.common.SpringContextHolder;
import com.xmhmyh.generalframework.framework.redis.IredisService;
import com.xmhmyh.generalframework.framework.redisQueue.consumers.RedisQueueConsumer;
import com.xmhmyh.generalframework.framework.redisQueue.consumers.RedisQueueConsumerContainer;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class RedisQueueListener  implements Runnable {
    private final Long WAITTIME = 30L;
    private RedisQueueConsumer redisQueueConsumer;

    IredisService redisService;

    public RedisQueueListener(RedisQueueConsumer redisQueueConsumer) {
        this.redisQueueConsumer = redisQueueConsumer;
    }

    @Override
    public void run() {
        redisService = SpringContextHolder.getBean(IredisService.class);
        log.info("redis监听器开始监听:{}", redisQueueConsumer.getQueueName());
        while (RedisQueueConsumerContainer.isRun) {
            try {
                Object object = redisService.rPop(redisQueueConsumer.getQueueName(),WAITTIME);
                if (null!=object && object instanceof RedisQueueMessage) {
                    redisQueueConsumer.getMessage((RedisQueueMessage) object);
                }
            } catch (Exception e) {
                e.printStackTrace();
                log.info("redis监听器错误：{}", redisQueueConsumer.getQueueName());
            }
        }

    }
}
